/*
 * Tomer Nuni 300585312
 * Idan Meshita 300043726
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class ViewManager {

	private int originX;
	private int originY;
	
	private double direction ;
	


	private int sizeWindowWidth;
	private int sizeWindowHeight;
	
	private int resoultionWindowWidth;
	private int resoultionWindowHeight;
	
	private double[][] viewMatix;

	
	/**
	 * This method initiate the view manager by loading the data form
	 * the file and build the matrix
	 * 
	 * @throws IOException 
	 */
	public void init(String viewFile) throws IOException { 
		loadDataFromFile(viewFile);
		buildViewMatrix();
		
		
	}


	/**
	 * 
	 * This method loads all the data from the file and filling the private members
	 * 
	 * @throws IOException
	 */
	private void loadDataFromFile(String fileName) throws IOException {
		BufferedReader r = new BufferedReader(new FileReader(fileName));
		String line = r.readLine();
		
		String[] st=  line.split(" ");
		originX= Integer.parseInt(st[1]);
		originY= Integer.parseInt(st[2]);
		//read next line 
		line = r.readLine();
		st=  line.split(" ");
		direction = Double.parseDouble(st[1]);
		// read next line - size of the window
		line = r.readLine();
		st=  line.split(" ");
		sizeWindowWidth= Integer.parseInt(st[1]);
		sizeWindowHeight= Integer.parseInt(st[2]);
		// read next line - resoultion
		line = r.readLine();
		st=  line.split(" ");
		resoultionWindowWidth= Integer.parseInt(st[1]);
		resoultionWindowHeight= Integer.parseInt(st[2]);
		r.close();
		
	}


	/**
	 * 
	 * This method creates the view matrix by building the matrixes from 
	 * all the data
	 * 
	 */
	private void buildViewMatrix() {
		viewMatix = new double[][] {{1,0,0},{0,1,0},{0,0,1}};
		//make move to origen matrix
		double m1 [][]= {{1,0,-originX},
						{0,1,-originY},
						{0,0,1}};
		viewMatix= Matrix.multiplyMatrix(m1,viewMatix);
		//make turn angle matrix
		double cosA = Math.cos((-direction/360)*2*Math.PI);
		double sinA = Math.sin((-direction/360)*2*Math.PI);
		double m2[][] = {{cosA,sinA,0},
						{-sinA,cosA,0},	
						{0,0,1}};
		viewMatix= Matrix.multiplyMatrix(m2,viewMatix);
		//make normalize size matrix
		double m3 [][]= {{1/(double)sizeWindowWidth,0,0},
						{0,1/(double)sizeWindowHeight,0},
						{0,0,1}};
		viewMatix= Matrix.multiplyMatrix(m3,viewMatix);
		//Stretch to regulation
		double m4 [][]= {{resoultionWindowWidth,0,0},
						{0,resoultionWindowHeight,0},
						{0,0,1}};
		viewMatix= Matrix.multiplyMatrix(m4,viewMatix);
		//change the y axis
		double m5 [][]= {{1,0,0},
						{0,-1,0},
						{0,0,1}};
		viewMatix= Matrix.multiplyMatrix(m5,viewMatix);
		//move the screen down after -y
		double m6 [][]= {{1,0,0},
						{0,1,resoultionWindowHeight+(Constants.FRAM_POSITION*2)},
						{0,0,1}};
		viewMatix= Matrix.multiplyMatrix(m6,viewMatix);
		//moving the origin to the left button corner of the FRAME
		double m7 [][]= {{1,0,Constants.FRAM_POSITION},
						{0,1,Constants.FRAM_POSITION},
						{0,0,1}};
		viewMatix= Matrix.multiplyMatrix(m7,viewMatix);
	}
	/**
	 * 
	 * @return the resolution width
	 */
	public int getResoultionWindowWidth() {
		return resoultionWindowWidth;
	}

	/**
	 * 
	 * @return the resolution height
	 */
	public int getResoultionWindowHeight() {
		return resoultionWindowHeight;
	}

	/**
	 * 
	 * @return the view matrix- that will transform all the points form the real
	 * world to points on the screen
	 */
	public double[][] getViewMatix() {
		return viewMatix;
	}
	/**
	 * makes the view manager build the viewmatrix again
	 * given the new resoultion of the screen
	 * 
	 * @param width
	 * @param height
	 */
	public void resetResolution(int width, int height) {
		resoultionWindowWidth=width;
		resoultionWindowHeight=height;
		buildViewMatrix();
		
	}

}
